package work.work60;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 学生管理程序的界面
 */
public class StudentUI {
    private SuperArray ary = new SuperArray();
    private Scanner sc = new Scanner(System.in);

    public StudentUI() {
        menu();
    }

    /**
     * 菜单界面
     */
    public void menu() {
        System.out.println("欢迎使用学生管理程序");
        while (true) {
            System.out.println("请选择：1-查看所有学生 2-增加 3-修改 4-删除 5-退出 6-查找");
            int input = 0;
            try {
                input = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入错误，请从新输入");
                sc.next();
                continue;
            }
            if (input == 1) {
                show();
            } else if (input == 2) {
                add();
            } else if (input == 3) {
                update();
            } else if (input == 4) {
                remove();
            } else if (input == 5) {
                return;
            } else if (input == 6) {
                find();
            } else {
                System.out.println("输入错误");
            }
        }
    }

    /**
     * 显示所有学生
     */
    public void show() {
        for (int i = 0; i < ary.size(); i++) {
            Student student = (Student) ary.get(i);
            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
        }
    }

    /**
     * 增加学生
     */
    public void add() {
        String name = null;
        String gender = null;
        int age = 0;
        while (true) {
            System.out.println("请输入学生的姓名：");
            try {
                name = sc.next();
            } catch (InputMismatchException e) {
                System.out.println("输入错误，请重新输入");
                sc.next();
                continue;
            }
            break;
        }
        while (true) {
            System.out.println("请输入学生性别：");
            try {
                gender = sc.next();
            } catch (InputMismatchException e) {
                System.out.println("输入错误，请重新输入");
                sc.next();
                continue;
            }
            break;
        }
        while (true) {
            System.out.println("请输入学生年纪：");
            try {
                age = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入错误，请重新输入");
                sc.next();
                continue;
            }
            break;
        }
        ary.add(new Student(name, gender, age));
    }

    /**
     * 修改学生信息
     */
    public void update() {
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        for (int i = 0; i < ary.size(); i++) {
            Student student = (Student) ary.get(i);
            if (student.getName().equals(name)) {
                System.out.println("请输入要修改的信息:");
                while (true) {
                    int input = 0;
                    while (true) {
                        System.out.println("1-姓名 2-性别 3-年纪");
                        try {
                            input = sc.nextInt();
                        }catch (InputMismatchException e){
                            System.out.println("输入错误，请重新输入");
                            sc.next();
                            continue;
                        }
                        break;
                    }
                    if (input == 1) {
                        System.out.println("请输入修改学生后的姓名：");
                        String newname = sc.next();
                        student.setName(newname);
                        System.out.println("请问还需要继续修改吗？");
                        int newinput = 0;
                        while (true) {
                            System.out.println("1-继续 2-退出");
                            try {
                                newinput = sc.nextInt();
                            }catch (InputMismatchException e){
                                System.out.println("输入错误，请重新输入");
                                sc.next();
                                continue;
                            }
                            break;
                        }
                        if (newinput == 2) {
                            return;
                        }
                    } else if (input == 2) {
                        System.out.println("请输入修改学生后的性别：");
                        String gender = sc.next();
                        student.setGender(gender);
                        System.out.println("请问还需要继续修改吗？");
                        int newinput = 0;
                        while (true) {
                            System.out.println("1-继续 2-退出");
                            try {
                                newinput = sc.nextInt();
                            }catch (InputMismatchException e){
                                System.out.println("输入错误，请重新输入");
                                sc.next();
                                continue;
                            }
                            break;
                        }
                        if (newinput == 2) {
                            return;
                        }
                    } else if (input == 3) {
                        System.out.println("请输入修改学生后的年龄");
                        int age = sc.nextInt();
                        student.setAge(age);
                        System.out.println("请问还需要继续修改吗？");
                        int newinput = 0;
                        while (true) {
                            System.out.println("1-继续 2-退出");
                            try {
                                newinput = sc.nextInt();
                            }catch (InputMismatchException e){
                                System.out.println("输入错误，请重新输入");
                                sc.next();
                                continue;
                            }
                            break;
                        }
                        if (newinput == 2) {
                            return;
                        }
                    } else {
                        System.out.println("输入错误");
                    }
                }
            }
        }
        System.out.println("学生姓名输入错误");
    }

    /**
     * 删除学生
     */
    public void remove() {
        System.out.println("请输入删除学生的名字：");
        String name = sc.next();
        for (int i = 0; i < ary.size(); i++) {
            Student student = (Student) ary.get(i);
            if (student.getName().equals(name)) {
                ary.remove(i);
                return;
            }
        }
        System.out.println("输入学生名字错误");
    }

    /**
     * 查找
     */
    public void find() {
        int input = 0;
        int index = 0;
        while (true) {
            System.out.println("请输入查找信息");
            System.out.println("1-姓名 2-性别 3-年纪");
            try {
                input = sc.nextInt();
            }catch (InputMismatchException e){
                System.out.println("输入错误，请重新输入");
                sc.next();
                break;
            }
            if (input == 1){
                System.out.println("请输入名字：");
                String name = sc.next();
                for (int i = 0; i < ary.size(); i++){
                    Student student = (Student) ary.get(i);
                    if (student.getName().equals(name)){
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        index++;
                    }
                    if (index == 0){
                        System.out.println("没有该姓名的学生");
                        return;
                    }
                }
            }
            if (input == 2){
                System.out.println("请输入性别:");
                String gender = sc.next();
                for (int i = 0; i < ary.size(); i++){
                    Student student = (Student) ary.get(i);
                    if (student.getGender().equals(gender)){
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                    index++;
                    }
                    if (index == 0){
                        System.out.println("性别输入错误");
                        return;
                    }
                }
            }
            if (input == 3){
                int age = 0;
                while (true) {
                    System.out.println("请输入年纪:");
                    try {
                        age = sc.nextInt();
                    }catch (InputMismatchException e){
                        System.out.println("输入错误，请重新输入");
                        sc.next();
                        continue;
                    }
                    break;
                }
                for (int i = 0; i < ary.size(); i++){
                    Student student = (Student) ary.get(i);
                    if (student.getAge() == age){
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                    index++;
                    }
                    if (index == 0){
                        System.out.println("没有该年纪的学生");
                        return;
                    }
                }
            }
            return;
        }
    }

    public static void main(String[] args) {
        new StudentUI();
    }
}
